<?php
namespace App\Service\H5;

class WhitemService20250921 extends BaseService
{
    //项目工时

    //详情
    public static function getOne($id, $pas)
    {
        $arys = ['data' => [], 'whList' => [], 'hour' => []];
        $res = (array) self::dbConn(self::$dbYwWhItem)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            $arys['data'] = $res;
            $configOne = (array) self::dbConn(self::$dbConfigWorkHour)
                ->where('id', $arys['data']['config_whid'])
                ->first();
            $arys['data']['config_title'] = empty($configOne) ? '' : $configOne['title'];
        }
        $arys['whList'] = self::dbConn(self::$dbConfigWorkHour)
            ->select('id', 'title')
            ->where('del', 0)
            ->where('depart_id', $pas['depart_id'])
            ->get();
        $arys['cntList'] = self::dbConn(self::$dbConfigWhCnt)
            ->select('id', 'cnt as title')
            ->where('del', 0)
            ->where('wh_id', $arys['data']['config_whid'])
            ->get();
        //时间
        for ($i = 8; $i <= 12; $i ++) {
            if ($i == 12) {
                $arys['hour'][] = '12:00';
            } else {
                $ii = strlen($i)<2 ? '0'.$i : $i;
                for ($j = 0; $j <= 5; $j ++) {
                    $jj = $j == 0 ? '00' : $j * 10;
                    $arys['hour'][] = $ii.':'.$jj;
                }
            }
        }
        for ($i = 13; $i <= 17; $i ++) {
            if ($i == 17) {
                $arys['hour'][] = '17:00';
            } else {
                $ii = strlen($i)<2 ? '0'.$i : $i;
                for ($j = 0; $j <= 5; $j ++) {
                    $jj = $j == 0 ? '00' : $j * 10;
                    $arys['hour'][] = $ii.':'.$jj;
                }
            }
        }
        return self::toback(0, '', $arys);
    }

    //新增
    public static function add($pas)
    {
        $pas['addtime'] = $pas['uptime'] = toDate();
        $res = self::dbConn(self::$dbYwWhItem)->insertGetId($pas);
        if (!$res) {
            return self::toback(-1, 'error');
        }
        return self::toback();
    }

    //修改
    public static function modify($id, $pas)
    {
        $res = self::dbConn(self::$dbYwWhItem)->where('id', $id)->first();
        if (empty($res)) {
            return self::toback(-2, '数据不存在');
        }
        $pas['uptime'] = toDate();
        $res = self::dbConn(self::$dbYwWhItem)
            ->where('id', $id)
            ->update($pas);
        if (!$res) {
            return self::toback(-1, 'error');
        }
        return self::toback();
    }

    //删除
    public static function del($id)
    {
        $res = self::dbConn(self::$dbYwWhItem)->where('id', $id)->first();
        if (empty($res)) {
            return self::toback(-2, '数据不存在');
        }
        $res = self::dbConn(self::$dbYwWhItem)
            ->where('id', $id)
            ->update(['del' => 1, 'deltime' => toDate()]);
        if (!$res) {
            return self::toback(-1, 'error');
        }
        return self::toback();
    }
}